/**
 * 
 */
package org.i1510.base.utils.file;

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;

/**
 * @author rambo
 * 剪切板工具
 *
 */
public class ClipboardUtils {
	public static void setClipboardImage(final Image image) {
		Transferable trans = new Transferable() {
			public DataFlavor[] getTransferDataFlavors() {
				return new DataFlavor[] { DataFlavor.imageFlavor };
			}

			public boolean isDataFlavorSupported(DataFlavor flavor) {
				return DataFlavor.imageFlavor.equals(flavor);
			}

			public Object getTransferData(DataFlavor flavor)
					throws UnsupportedFlavorException, IOException {
				if (isDataFlavorSupported(flavor))
					return image;
				throw new UnsupportedFlavorException(flavor);
			}

		};
		Toolkit.getDefaultToolkit().getSystemClipboard()
				.setContents(trans, null);
	}

	public static void main(String[] args) {
		URL url;
		try {
//			url = new URL("https://a-ssl.duitang.com/uploads/item/201211/24/20121124112047_KUFxK.thumb.700_0.gif");
//			// 载入图片到输入流
//			java.io.BufferedInputStream bis = new BufferedInputStream(url.openStream());
			
			
			File file = new File("/Users/rambo/Downloads/s.jpg");
			
//			BufferedImage src2 = ImageIO.read(file); // 读入文件
					
			BufferedImage bi = ImageIO.read(file);
			Image im = (Image) bi;
			setClipboardImage(im);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException s) {
			// TODO Auto-generated catch block
			s.printStackTrace();
		}

	}
}
